package com.company.test16;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * Created by Administrator on 2016.6.19.
 */
public class Test16_10 extends JFrame{
    private KeyboardPanel keyboardPanel=new KeyboardPanel();

    public Test16_10(){
        add(keyboardPanel);

        keyboardPanel.setFocusable(true);
    }

    public static void main(String[] args){
        Test16_10 frame=new Test16_10();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setTitle("Just a test");
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    static class KeyboardPanel extends JPanel{
        private int x=100;
        private int y=100;
        private char keyChar='A';

        public KeyboardPanel(){
            addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    switch (e.getKeyCode()){
                        case KeyEvent.VK_DOWN: y+=10;break;
                        case KeyEvent.VK_UP: y-=10;break;
                        case KeyEvent.VK_LEFT:x-=10;break;
                        case KeyEvent.VK_RIGHT:x+=10;break;
                        default:keyChar=e.getKeyChar();
                    }
                    repaint();
                }
            });
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setFont(new Font("TimeRoman",Font.PLAIN,24));
            g.drawString(String.valueOf(keyChar),x,y);
        }
    }
}
